package com.yanzhu.service.impl;

import com.yanzhu.service.GrabRedPacketsService;
import com.yanzhu.service.mapper.GrabRedPacketsMapper;
import com.yanzhu.service.mapper.MessageMapper;
import com.yanzhu.service.mapper.RedPacketsMapper;
import com.yanzhu.service.mapper.WalletMapper;
import com.yanzhu.service.model.GrabRedPackets;
import com.yanzhu.service.model.Message;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: 方炎柱
 * @Description:
 * @Date: Created in 16:15 2018/1/29
 * @Modified:
 */
@AllArgsConstructor
@Service
public class GrabRedPacketsServiceImpl implements GrabRedPacketsService {
    private final GrabRedPacketsMapper grabRedPacketsMapper;

    private final RedPacketsMapper redPacketsMapper;

    private final WalletMapper walletMapper;

    private final MessageMapper messageMapper;

    @Override
    @Transactional
    public void grabRedPackets(GrabRedPackets grabRedPackets, long vid, float money, Message message, Message message1) {
        grabRedPacketsMapper.insert(grabRedPackets);
        redPacketsMapper.updateRedPackets(vid, money);
        walletMapper.updateAfterGrabPackets(grabRedPackets.getUid(), money);
        messageMapper.insert(message);
        if(message1 != null)
            messageMapper.insert(message1);
    }

    @Override
    public int insert(GrabRedPackets grabRedPackets) {
        return grabRedPacketsMapper.insert(grabRedPackets);
    }

    @Override
    public GrabRedPackets queryRedPacketsByUid(long uid) {
        return grabRedPacketsMapper.queryRedPacketsByUid(uid);
    }
}
